local ObjHuman = require("core.ObjHuman")
local SkillExcel = require("excel.Skill").Skill
local Loss = require("skill.Loss")				-- 消耗
local ShenbingExcel = require("excel.Shenbing").shenbing
local PetExcel = require("excel.Pet").pet
local ShenbingLogic = require("shenbing.ShenbingLogic")
local Log = require("common.Log")
local HorseConfig = require("excel.Horse")
local StageConfig = HorseConfig.stage
local HuanhuaConfig = HorseConfig.huanhua
local HorseLogic = require("horse.HorseLogic")
local BaowuConfig = require("excel.Baowu").baowu
local BaowuLogic = require("baowu.BaowuLogic")
local PetLogic = require("pet.PetLogic")
local QiliaoConfig = require("excel.Qiliao")

local function isSbActivation(human,skillID) -- 神兵技能是否激活 
  for i = 0, human.db.shenbingLen - 1  do
      local data = human.db.shenbing[i]
      local sb  = ShenbingExcel[data.id]
      for k,v in pairs(sb.haveSkill) do
          if v == skillID and data.skillLv[k-1] > 0 then
              return data, k, data.skillLv[k-1]
          end
      end
  end      
end

local function isPetActivation(human, skillID) -- 神将技能是否激活
  for i = 0, human.db.petLen-1 do
      local data = human.db.pet[i]
      local config = PetExcel[data.id]
      for k,v in pairs(config.skillId) do
          if v == skillID and data.skillLv[k-1] > 0 then
              return data, k, data.skillLv[k-1]
          end
      end
  end  
end

local function getPetId(human, skillId)
  for i=1, #PetExcel do
    for j=1, #PetExcel[i].skillId do
      if PetExcel[i].skillId[j] == skillId then
        return i
      end
    end
  end  
end

local function isHorseActiation(human, skillID)
  if StageConfig[1].skillId == skillID then
    return nil, 1, human.db.horseSkillLv
  end
  for k,v in pairs(HuanhuaConfig) do
    if v.skillId == skillID then
      local data = HorseLogic.getHuanhuaHorseInfo(human, k) 
      return data, 2, data.skillLv
    end
  end 
end

local function isBaowuActiation(human, id)
  for i=0, human.db.baowuLen - 1 do
    if human.db.baowu[i].skillId == id then
      return human.db.baowu[i], human.db.baowu[i].id, human.db.baowu[i].skillLv
    end
  end
end

function upgrade(human,id, from)			-- 升级
	local skillConfig = SkillExcel[id]
	local db, index, level 
	if from == 0 then
	   db, index,level = isSbActivation(human, id)
	elseif from == 1 then
	   db, index,level = isPetActivation(human, id)
	elseif from == 2 then
	   db, index, level = isHorseActiation(human, id)
	elseif from == 3 then
	   db, index, level = isBaowuActiation(human, id)
	end
	if level == nil then	-- 未激活 无法升级
		return false,"未激活无法升级"
	end
	if not skillConfig.upgradeCondition[level] then
		return false,"技能升级未配置"
	end
	local flag,ret = Loss.handler(human,skillConfig.upgradeCondition[level], id)
	if flag then
	  if from ~= 2 and from ~= 3 then
		  db.skillLv[index-1] = level+1
		end
		ObjHuman.doCalc(human)
		
		if from == 0 then
        ShenbingLogic.onSkillUpGrade(human, db.id, id, level+1)
    elseif from == 1 then
        PetLogic.onSkillUpGrade(human, index, id)
    elseif from == 2 then
        HorseLogic.onSkillUpGrade(human, index, id)
    elseif from == 3 then
        BaowuLogic.onSkillUpGrade(human, index, id)
    else
        Log.write(Log.LOGID_OSS_PET_SKILL_UP,human._id,human.account,human.name,human.db.lv,getPetId(human, id),id,level,level+1)  
    end
	end	
	return flag,ret
end


function getLevel(human,skillID)
	local skillConfig = SkillExcel[skillID]
  if skillConfig.group == 0 then
      return 1
  end
  
  local data = ObjHuman.getShenbing(human, human.db.putonShenbing)
  local sb  = ShenbingExcel[human.db.putonShenbing]
  if data and sb then
      for k,v in pairs(sb.haveSkill) do
          if v == skillConfig.group then
              return data.skillLv[k-1] > 0 and data.skillLv[k-1] or 1
          end
      end
  end
  
  for i = 0, human.db.shenbingLen - 1  do
       if human.db.shenbing[i].id ~= human.db.putonShenbing then
          data = human.db.shenbing[i]
          sb  = ShenbingExcel[data.id]
          for k,v in pairs(sb.haveSkill) do
             if v == skillConfig.group then
              return data.skillLv[k-1] > 0 and data.skillLv[k-1] or 1
             end
          end
      end
  end
  
  for i = 0, human.db.petLen -1 do
    for j=1, #PetExcel[human.db.pet[i].id].skillId do
      if PetExcel[human.db.pet[i].id].skillId[j] == skillID then
        if human.db.pet[i].skillLv[j - 1] == 0 then
          return 1
        else
          return human.db.pet[i].skillLv[j - 1]
        end
      end
    end
  end
  skillID = skillConfig.group
  if StageConfig[1].skillId == skillID then
    return human.db.horseSkillLv
  end
  for k,v in pairs(HuanhuaConfig) do
    if v.skillId == skillID then
      local info = HorseLogic.getHuanhuaHorseInfo(human, k)
      if info then
        return info.skillLv
      end
    end
  end 
  
  for i=0, human.db.baowuLen - 1 do
    if human.db.baowu[i].skillId == skillID or BaowuConfig[human.db.baowu[i].id].skillId == skillID or BaowuConfig[human.db.baowu[i].id].leftSkill[1][1] == skillID or 
    BaowuConfig[human.db.baowu[i].id].leftSkill[2][1] == skillID or BaowuConfig[human.db.baowu[i].id].rightSkill[1][1] == skillID
    or BaowuConfig[human.db.baowu[i].id].rightSkill[2][1] == skillID then
      return human.db.baowu[i].skillLv
    end
  end
  
  if skillID == QiliaoConfig.other[1].skillId then
    return human.db.QiliaoSkillLv > 0 and human.db.QiliaoSkillLv or 1 
  end
      
	return 1
end
